import java.util.*;

/**
 * 
 * @author Pietro Bua & Rohmin Mirza
 *
 */
public class Node
{
	private ArrayList<Node> nodes = new ArrayList<Node>();

	private char myC;
	private Data myData;
	
	/**
	 * 
	 * @param ch
	 */
	public Node( char ch )
	{
		myC = ch;
	}
	
	/**
	 * 
	 * @param s
	 * @param d
	 */
	public void insert( String s , Data d )
	{
		if( (! s.equals(""))&&( ! s.isEmpty() ) )
		{
			System.out.println( "Processing [" + s + "] " /*+ d.toString()*/ ); // Debug
			Node node = getNode(s.charAt(0));
			if( node == null )
			{
				node = new Node( s.charAt(0) );
				nodes.add( node );
			}
			
			if(s.length()==1)
			{
				if(node.getData() == null)
				{
					node.setData( new Data(d.getValue()) );
				}
				node.getData().setValue(d.getValue());
				node.getData().addPos();
			}
			String strWithoutFirst = s.substring(1, s.length());
			node.insert(strWithoutFirst, d );
		}
		else
		{
			//System.out.println("No chars in String anymore");
		}
	}	
	
	/**
	 * 
	 * @param data
	 */
	public void setData( Data data )
	{
		myData = data;
	}
	
	/**
	 * 
	 * @return
	 */
	public Data getData()
	{
		return myData;
	}
	
	/**
	 * 
	 * @param ch
	 * @return
	 */
	public Node getNode( char ch )
	{
		for(Node n: nodes)
		{
			if( n.getName() == ch )
			{
				return n;
			}
		}
		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	public ArrayList<Node> getNodes( )
	{
		return nodes;
	}
	
	/**
	 * 
	 * @return
	 */
	public char getName()
	{
		return myC;
	}

	/**
	 * 
	 * @param s
	 * @return
	 */
	public Data search( String s )
	{
		for(Node n: nodes)
		{
			if(s.length()>0)
			{
				//if( ( n.myC == s.charAt(s.length()-1))&&(n.getNodes().isEmpty()) )
				if( (s.length() <=1)&&(n.myC == s.charAt(s.length()-1)) )
				{
					return n.getData();
				}
				else if(n.myC == s.charAt(0))
				{
					String str = s.substring(1,s.length());
					return n.search(str);
				}
			}
		}
		return null;
	}
	
	/**
	 * 
	 * @param s
	 */
	public void delete( String s )
	{
		Data toDelete = search(s);
		if((toDelete != null))
		{
			if((toDelete.getPositionsCount()>0))
			{
				search(s).removeOne();
			}
		}
	}

	/**
	 * 
	 * @param x
	 * @return
	 */
	public ArrayList<String> getAllNodes( String x, ArrayList<String> nodeWords )
	{
		x += this.myC + "";

		Data data = this.getData();
		if( data != null )
		{
			for( int i=0; i<data.getPositionsCount();i++)
			{
				nodeWords.add( x.substring(1,x.length()) );
			}
		}
		
		for(Node n: nodes)
		{
			n.getAllNodes( x , nodeWords);
		}
		return nodeWords;
	}
	
	/**
	 * 
	 */
	public String toString()
	{
		String s = this.getName() + "";
		if(nodes.size()>0)
		{
			for(Node n: nodes)
			{
				String strData = "";
				if(myData != null)
				{
					strData = myData.toString();
				}

				s = "" + s + " " + strData + "(" + n.toString() + " " +  ")";
			}
		}
		else
		{
			s+= " [" +myData + "] ";
		}
		return s;
	}
}
